package com.scu.bank.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scu.bank.model.dao.AcademicActivitieDao;
import com.scu.bank.model.entity.AcademicActivitieEntity;
import com.scu.bank.model.example.AcademicActivitieExample;
import com.scu.bank.model.pojo.AcademicActivitie;
import com.scu.bank.model.base.BaseServiceImpl;
import com.scu.bank.model.pojo.UserInfo;
import com.scu.bank.service.AcademicActivitieService;
import com.scu.bank.service.UserInfoService;
import com.scu.bank.util.ListUtils;
import com.scu.bank.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by pmeng on 2021/01/05.
 */
@Service
@Transactional
public class AcademicActivitieServiceImpl extends BaseServiceImpl<AcademicActivitie,AcademicActivitieDao> implements AcademicActivitieService {
    @Autowired
    private AcademicActivitieDao tAcademicActivitieDao;
    @Autowired
    private UserInfoService userInfoService;


    @Override
    public AcademicActivitieDao getDao() {
        return tAcademicActivitieDao;
    }

    @Override
    public PageInfo<AcademicActivitie> list(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        AcademicActivitieExample example = new AcademicActivitieExample();
        example.createCriteria().andIsDelEqualTo(false);
        PageInfo<AcademicActivitie> pageInfo = new PageInfo(tAcademicActivitieDao.selectByExample(example));
        List<AcademicActivitie> list = pageInfo.getList();

        if (!ListUtils.isEmpty(list)){
            list.forEach((temp)->{
                UserInfo userInfo = userInfoService.getById(temp.getSendUser());
                temp.setSendName(userInfo == null ? "" : userInfo.getUserName());
            });
        }
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public void addAcademicActivitie(AcademicActivitie academicActivitie) {
        academicActivitie.setSendUser(WebUtil.getCurrentUserId());
        academicActivitie.setStatus("0");
        save(academicActivitie);
    }
}
